#include "StatusDisplayWidget.h"
#include "LabeledValueBox/LabeledValueBox.h"

#include <QHBoxLayout>

StatusDisplayWidget::StatusDisplayWidget(QWidget *parent) : QWidget(parent)
{
    initUI();
}

void StatusDisplayWidget::initUI()
{
    setStyleSheet("background-color: rgba(0, 0, 0, 30); border-radius: 8px;");
    setAttribute(Qt::WA_TransparentForMouseEvents, true);  // 鼠标事件穿透
    setAttribute(Qt::WA_StyledBackground, true);           // 添加这行确保背景显示
    setFixedHeight(60);

    // 创建状态栏内容
    QWidget *statusContent = new QWidget(this);
    QHBoxLayout *statusLayout = new QHBoxLayout(statusContent);
    statusLayout->setContentsMargins(6, 3, 6, 3);  // 增加内边距
    statusLayout->setSpacing(3);                   // 增加间距

    // 创建状态显示组件
    m_depthDisplay = new LabeledValueBox("深度", statusContent);
    m_heightDisplay = new LabeledValueBox("高度", statusContent);
    m_compassDisplay = new LabeledValueBox("罗盘", statusContent);
    m_pitchStateDisplay = new LabeledValueBox("俯仰状态", statusContent);
    m_pitchDisplay = new LabeledValueBox("俯仰", statusContent);
    m_rollStateDisplay = new LabeledValueBox("横滚状态", statusContent);
    m_rollDisplay = new LabeledValueBox("横滚", statusContent);
    m_cameraAngleDisplay = new LabeledValueBox("云台", statusContent);
    m_sonarDisplay = new LabeledValueBox("声呐", statusContent);
    m_waterTempDisplay = new LabeledValueBox("水温", statusContent);
    m_temperatureDisplay = new LabeledValueBox("舱温", statusContent);
    m_cabinHumidityDisplay = new LabeledValueBox("湿度", statusContent);
    m_waterLeakDisplay = new LabeledValueBox("漏水", statusContent);

    // 设置组件高度适应状态栏
    QList<LabeledValueBox *> statusWidgets = {
        m_depthDisplay, m_heightDisplay, m_compassDisplay, m_pitchStateDisplay, m_pitchDisplay, m_rollStateDisplay, m_rollDisplay, m_cameraAngleDisplay, m_sonarDisplay, m_waterTempDisplay, m_temperatureDisplay, m_cabinHumidityDisplay, m_waterLeakDisplay};

    //// 设置状态栏专用样式
    //QString stateBarLabelStyle(
    //    "color: white;"
    //    "font-weight: normal;"
    //    "background: transparent;");

    //QString stateBarValueStyle(
    //    "color: white;"  // 纯白色，突出显示
    //    "font-weight: bold;"
    //    "background: transparent");

    //QString stateBarBoxStyle(
    //    "background-color: rgba(30, 30, 30, 150);"  // 深色半透明背景
    //    "border: 3px solid rgba(100, 100, 100, 150);"
    //    "border-radius: 8px;"  // 更大的圆角
    //    "padding: 0px;");

        // 设置状态栏专用样式 - 使用完整的样式表
    QString stateBarBoxStyle(
        "LabeledValueBox {"
        "background-color: rgba(30, 30, 30, 150);"
        "border: 3px solid rgba(100, 100, 100, 150);"
        "border-radius: 8px;"
        "padding: 4px 8px;"
        "}"
        "QLabel {"
        "background: transparent;"
        "border: none;"
        "padding: 0px;"
        "margin: 0px;"
        "}");


    for (auto widget : statusWidgets)
    {
        widget->setFixedHeight(42);   // 稍微增加高度
        widget->setMinimumWidth(75);  // 设置最小宽度避免挤压

        // 应用状态栏专用样式
        //widget->setLabelStyle(stateBarLabelStyle);
        //widget->setValueStyle(stateBarValueStyle);
        widget->setBoxStyle(stateBarBoxStyle);

        widget->setLabelWidth(30);  // 设置标签宽度
        widget->setLabelAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
        widget->setValueAlignment(Qt::AlignVCenter | Qt::AlignLeft);

        // 设置字体大小
        widget->setLabelFontSize(11);
        widget->setValueFontSize(12);
    }

    // 使用弹簧实现均匀分布
    statusLayout->addStretch();  // 左侧弹簧
    statusLayout->addWidget(m_depthDisplay);
    statusLayout->addStretch();  // 左侧弹簧
    statusLayout->addWidget(m_heightDisplay);
    statusLayout->addStretch();  // 左侧弹簧
    statusLayout->addWidget(m_compassDisplay);
    statusLayout->addStretch();  // 中间弹簧1
    statusLayout->addWidget(m_pitchStateDisplay);
    statusLayout->addWidget(m_pitchDisplay);
    statusLayout->addWidget(m_rollStateDisplay);
    statusLayout->addWidget(m_rollDisplay);
    statusLayout->addStretch();  // 中间弹簧2
    statusLayout->addWidget(m_cameraAngleDisplay);
    statusLayout->addWidget(m_sonarDisplay);
    statusLayout->addWidget(m_waterTempDisplay);
    statusLayout->addStretch();  // 中间弹簧3
    statusLayout->addWidget(m_temperatureDisplay);
    statusLayout->addWidget(m_cabinHumidityDisplay);
    statusLayout->addWidget(m_waterLeakDisplay);
    statusLayout->addStretch();  // 右侧弹簧

    // 设置状态栏的主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addWidget(statusContent);
}

QSize StatusDisplayWidget::sizeHint() const
{
    return QSize(800, 60);  // 默认尺寸
}

void StatusDisplayWidget::updateStatusDisplay(/*const vvl::proto::payload_mcu_data_full &data*/)
{
    // const auto &resp_40 = data.resp_vvl40;

    m_depthDisplay->setValue(QString::number(123.456, 'f', 1));
    m_heightDisplay->setValue(QString::number(456.456, 'f', 1));
    m_compassDisplay->setValue(QString::number(789.456, 'f', 1));
    m_pitchStateDisplay->setValue("前倾");
    m_pitchDisplay->setValue(QString::number(101.456, 'f', 1));
    m_rollStateDisplay->setValue("右倾");
    m_rollDisplay->setValue(QString::number(102.456, 'f', 1));
    m_cameraAngleDisplay->setValue(QString::number(103.456, 'f', 1));
    m_sonarDisplay->setValue(QString::number(104, 'f', 1));
    m_waterTempDisplay->setValue(QString::number(105.456, 'f', 1));
    m_temperatureDisplay->setValue(QString::number(106.456, 'f', 1));
    m_cabinHumidityDisplay->setValue(QString::number(10.4567, 'f', 1));

    if (1 > 100)
    {
        m_waterLeakDisplay->setValue("检测到漏水");
    }
    else
    {
        m_waterLeakDisplay->setValue("正常");
    }
}